<html>
<head>
	<title>Inventory Update</title>
	<script src="../lib/jquery-1.11.2.min.js" type="text/javascript"></script>
</head>
<body>

<script type="text/javascript">
	
	// Example inventory lists
	var curInv = [
	    [21, "Bowling Ball"],
	    [2, "Dirty Sock"],
	    [1, "Hair Pin"],
	    [5, "Microphone"]
	];

	var newInv = [
	    [2, "Hair Pin"],
	    [3, "Half-Eaten Apple"],
	    [67, "Bowling Ball"],
	    [7, "Toothpaste"]
	];

	function getLib(arr1) {
	    // All inventory must be accounted for or you're fired!
	  var lib = [];
	  for(var i=0;i<arr1.length;i++){
	    lib.push(arr1[i][1]);
	  }
	  return lib;
	}

	function updateInventory(arr1, arr2) {
	    // All inventory must be accounted for or you're fired!
	  curInv = arr1;
	  newInv = arr2;
	  var lib1 = getLib(curInv);
	  for(var i=0;i<newInv.length;i++){
	    var j = lib1.indexOf(newInv[i][1]);
	    if(j!=-1){
	      curInv[j][0] = curInv[j][0] + newInv[i][0];
	    }else {
	      curInv.push(newInv[i]);
	    }
	  }
	  curInv.sort(function(a,b){
	  	return a[1].charCodeAt(0)-b[1].charCodeAt(0);
	  });
	  return curInv;
	}


	var a = updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]);
	console.log(a.toString())
</script>
</body>
</html>